package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import com.yidu.ruifeng_logistics.entity.*;
import com.yidu.ruifeng_logistics.service.*;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.LogisMoneyVo;
import com.yidu.ruifeng_logistics.vo.LogisticsPredictionVo;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 下单表(LogisticsPrediction)表控制层
 *
 * @author 文湘
 * @since 2021-03-09 14:29:01
 */
@Api(description = "瑞丰物流客户下单接口")
@RestController
@RequestMapping("logisticsPrediction")
public class LogisticsPredictionController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsPredictionService logisticsPredictionService;

    /*
    地区
     */
    @Resource
    private LogisticsAreaService logisticsAreaService;

    /*
    货物
     */
    @Resource
    private LogisticsCargoService logisticsCargoService;

    /**
     * 员工选择角色
     */
    @Resource
    private LogisticsUserRoleService logisticsUserRoleService;

    /**
     * 角色表
     */
    @Resource
    private  LogisticsRoleService logisticsRoleService;

    /**
     * 快递员表
     */
    @Resource
    private LogisticsCourierService logisticsCourierService;

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除",notes = "输入Id")
    @PostMapping("deleteById")
    public BaseMessage deleteById(Integer id,Integer cId) {
        boolean b = logisticsPredictionService.deleteById(id);
        return b==true?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param pId 主键
     * @return 单条数据
     */
    @ApiOperation(value = "查询单条数据",notes = "根据ID查询")
    @PostMapping("selectOne")
    public BaseMessage selectOne(Integer pId,HttpSession session) {
        LogisticsPredictionVo vo = logisticsPredictionService.selectUpdateId(pId);
        // 取出 identity 的session
        BaseMessage message = new BaseMessage();
        Object obj = session.getAttribute("identity");
        if (obj!=null && obj!=""){
            //  判断是不是客户的session
            if ("client".equals(obj)){
                message.setState(0);
                message.setMsg("快递员才可以接单");
                return message;
            }else if ("staff".equals(obj)){
                LogisticsOrder order = new LogisticsOrder();
                //  取出员工的session
                LogisticsStaff staff = (LogisticsStaff) session.getAttribute("login");
                order.setCargoId(vo.getCargoId()); // 货物ID
                order.setClientId(vo.getClientId());// 客户ID
                order.setOrderBourn(vo.getPredictionBourn()); // 目的地
                order.setOrderDeparture(vo.getPredictionDeparture()); // 出发地
                order.setOrderPrice(Double.valueOf(vo.getPredictionMoney())); // 订单金额
                order.setStaffId(staff.getStaffId());  //员工ID
                order.setOrderState("待发件");  // 货物状态
                return BaseMessage.success(order);  // 返回新的订单表数据
            }
        }
        return BaseMessage.fail(); // 取不到session
    }


    /**
     * 分页显示所有信息
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页显示用户", notes = "分页返回所有用户信息")
    @GetMapping("findAll")
    public Map<String, Object> selectAll(Integer page, Integer limit,HttpSession session) {
        // 取出 identity 的session
        Object obj = session.getAttribute("identity");
        Map<String, Object> map = new HashMap();
        PageInfo<LogisticsPredictionVo> info =null;
        List<LogisticsPredictionVo> logisticsPredictionVos =null;
        //  判断取出的session 是否位空
        if (obj!=null && obj!=""){
            //  判断是不是客户的session
            if ("client".equals(obj)){
                //  取出客户的session
                LogisticsClient client = (LogisticsClient) session.getAttribute("login");
                //  将参数传入到分页助手中   PageHelper.startPage
                PageHelper.startPage(page,limit);
                //  根据客户ID 查询出与客户相关的订单显示
                logisticsPredictionVos = logisticsPredictionService.selectByIdClient(client.getClientId());
                //  查询到集合之后，将集合数据封装到分页助手的PageInfo中
                info = new PageInfo<>(logisticsPredictionVos);
                // foreach 循环
                for (LogisticsPredictionVo vo :logisticsPredictionVos){
                    // 通过 vo里面的目的地ID 查询地区表 找出地区名字
                    List<LogisticsArea> area = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionBourn()));
                    for(LogisticsArea area1:area){
                        //  通过循环 将查出来的名字放入到vo中 显示页面
                        vo.setPredictionBourn(area1.getAreaName());
                    }
                    // 通过 vo里面的出发地ID 查询地区表 找出地区名字
                    List<LogisticsArea> areas = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionDeparture()));
                    for(LogisticsArea area1:areas){
                        vo.setPredictionDeparture(area1.getAreaName());
                    }
                }
                map.put("msg", "客户");
                map.put("count", info.getTotal());
                map.put("data", logisticsPredictionVos);
                map.put("code", 0);
            }else if ("staff".equals(obj)){
                LogisticsRole logisticsRole = null;
                //  取出员工的session
                LogisticsStaff staff = (LogisticsStaff) session.getAttribute("login");
                // 根据员工ID查询出员工的角色ID
                List<LogisticsUserRole> logisticsUserRoles = logisticsUserRoleService.queryStaffIdReturnObj(staff.getStaffId());
                for (LogisticsUserRole role:logisticsUserRoles){
                     logisticsRole = logisticsRoleService.queryById(role.getRoleId());
                     if (logisticsRole.getRoleName().equals("管理员")){
                         //  如果是快递员 则显示所有数据源
                         PageHelper.startPage(page,limit);
                         logisticsPredictionVos = logisticsPredictionService.selectGuanLian();
                         //  查询到集合之后，将集合数据封装到分页助手的PageInfo中
                         info = new PageInfo<>(logisticsPredictionVos);
                         for (LogisticsPredictionVo vo :logisticsPredictionVos){
                             List<LogisticsArea> area = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionBourn()));
                             for(LogisticsArea area1:area){
                                 vo.setPredictionBourn(area1.getAreaName());
                             }
                             List<LogisticsArea> areas = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionDeparture()));
                             for(LogisticsArea area1:areas){
                                 vo.setPredictionDeparture(area1.getAreaName());
                             }
                         }
                         map.put("msg", "管理员");
                         map.put("count", info.getTotal());
                         map.put("data", logisticsPredictionVos);
                         map.put("code", 0);
                         break;   //  如果是管理员  就直接跳出
                     }else if(logisticsRole.getRoleName().equals("快递员")){

                         // 根据员工ID找出 当前快递员所属的地区
                         LogisticsCourier logisticsCourier = logisticsCourierService.selectStaffById(staff.getStaffId());
                         PageHelper.startPage(page,limit);
                         logisticsPredictionVos = logisticsPredictionService.selectAreaStaffId(logisticsCourier.getCourierArea());
                         //  查询到集合之后，将集合数据封装到分页助手的PageInfo中
                         info = new PageInfo<>(logisticsPredictionVos);
                         for (LogisticsPredictionVo vo :logisticsPredictionVos){
                             List<LogisticsArea> area = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionBourn()));
                             for(LogisticsArea area1:area){
                                 vo.setPredictionBourn(area1.getAreaName());
                             }
                             List<LogisticsArea> areas = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionDeparture()));
                             for(LogisticsArea area1:areas){
                                 vo.setPredictionDeparture(area1.getAreaName());
                             }
                         }
                         map.put("msg", "快递员");
                         map.put("count", info.getTotal());
                         map.put("data", logisticsPredictionVos);
                         map.put("code", 0);
                     }
                }
            }
        }
        return map;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑数据回显", notes = "根据ID查询")
    @GetMapping("selectUpdateId")
    public BaseMessage selectUpdateId(Integer id){
        LogisticsPredictionVo logisticsPredictionVo = logisticsPredictionService.selectUpdateId(id);
        return logisticsPredictionVo.getPredictionId()!=null?BaseMessage.success(logisticsPredictionVo):BaseMessage.fail();
    }


    @ApiOperation(value = "修改   or 下单", notes = "根据ID判断修改还是增加")
    @PostMapping("updateOrAdd")
    public BaseMessage updateOrAdd(LogisticsPredictionVo logisticsPredictionVo,
                                   HttpSession session){
        //  判断vo里面 下单表ID是否为空
        if (logisticsPredictionVo.getPredictionId()==null){
            // 为空就增加
            LogisticsPrediction insert = logisticsPredictionService.insert(logisticsPredictionVo, session);
            //  返回值不等于空 就ok
            return insert!=null?BaseMessage.success():BaseMessage.fail();
        }else {
            //  修改
            LogisticsPrediction update = logisticsPredictionService.update(logisticsPredictionVo);
            return update!=null?BaseMessage.success():BaseMessage.fail();
        }

    }


    @ApiOperation(value = "输入出发 目的 重量 查看价格", notes = "出发 目的 需要输入ID测试")
    @PostMapping("selectMoney")
    public BaseMessage selectMoney(LogisMoneyVo moneyVo){
            if (StringUtil.isNullOrEmpty(moneyVo.getPredictionBourn())){
                return BaseMessage.fail();
            }else {
                Double aDouble = logisticsPredictionService.selectMoney(moneyVo);
                return aDouble!=null?BaseMessage.success(aDouble):BaseMessage.fail();
        }
    }

}
